package com.cdy.jxc_project.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cdy.jxc_project.domain.TMenu;
import com.cdy.jxc_project.domain.TMenuRole;
import com.cdy.jxc_project.domain.TRole;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.req.AuthToRoleReq;
import com.cdy.jxc_project.service.TRoleService;
import com.cdy.jxc_project.utils.AddSysLog;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/25 9:23
 * */
@RestController
@RequestMapping("/tRole")
public class TRoleController {

    @Resource
    private TRoleService tRoleService;
    @AddSysLog
    @PostMapping("/queryTRoleList")
    public R queryTRoleList() {
        List<TRole> tRoleList = tRoleService.queryTRoleList();
        return R.ok().message("查询成功").data("roleList", tRoleList);
    }
    @AddSysLog
    @DeleteMapping("/deleteTRole/{id}")
    public R deleteTRole(@PathVariable("id") Integer id) {
        if (tRoleService.deleteTRole(id) > 0) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("错误");
        }
    }
    @AddSysLog
    @PostMapping("/insertAndUpdateTRole")
    public R insertAndUpdateTRole(@RequestBody TRole tRole) {
        try {
            if (tRole.getId() == null) {
                tRoleService.insertTRole(tRole);
                return R.ok().message("保存成功");
            } else {
                tRoleService.updateTRole(tRole);
                return R.ok().message("修改成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("程序出错");
        }
    }

    @PostMapping("/authToRole")
    public R authToRole(@RequestBody AuthToRoleReq req) {
        try {
            Integer rid = req.getRid();
            if (null == rid) {
                return R.error().message("角色id不能为空");
            } else {
                return tRoleService.authToRole(rid, req.getMids());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("程序出错");
        }
    }

    //查询角色的权限
    @PostMapping("/queryTRoleMenu/{id}")
    public R queryTRoleMenu(@PathVariable Integer id) {
        List<Integer> tRoleMenu = tRoleService.queryTRoleMenu(id);

        return R.ok().message("查询成功").data("tRoleMenuList", tRoleMenu);}
}
